package srtaller;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 *
 * @author DIN
 */
public class Autos {
    
    Conectate con;
    
    public Autos(){
        
        con = new Conectate();
    }
    
    public void NuevoAuto(String Marca, String Modelo, String Color, int Precio){
        try {
            PreparedStatement pstm = con.getConnection().prepareStatement("insert into " + 
                    "autos(Marca, Modelo, Color, Precio) " +
                    " values(?,?,?,?)");            
            pstm.setString(1, Marca);
            pstm.setString(2, Modelo);
            pstm.setString(3, Color);
            pstm.setInt(4, Precio);                                               
            pstm.execute();
            pstm.close(); 
            }
        catch(SQLException e){
         System.out.println(e);
      }
    }
    
     public Object [][] getDatos(){
      int registros = 0;
      try{         
         PreparedStatement pstm = con.getConnection().prepareStatement("SELECT count(1) as total FROM autos ");
         ResultSet res = pstm.executeQuery();
         res.next();
         registros = res.getInt("total");
         res.close();
      }catch(SQLException e){
         System.out.println(e);
      } 
      
   Object[][] data = new String[registros][5];  
     try{    
         PreparedStatement pstm = con.getConnection().prepareStatement("SELECT " +
            " Codigo,Marca,Modelo,Color,Precio" +
            " FROM autos" +
            " ORDER BY Codigo ");
         ResultSet res = pstm.executeQuery();
         int i = 0;
         while(res.next()){
            String estCodigo = res.getString("Codigo");
            String estMarca = res.getString("Marca");
            String estModelo = res.getString("Modelo");
            String estColor = res.getString("Color");
            String estPrecio = String.valueOf(res.getInt("Precio"));
           
            data[i][0] = estCodigo;
            data[i][1] = estMarca;            
            data[i][2] = estModelo;
            data[i][3] = estColor;
            data[i][4] = estPrecio;            
            i++;
         }
         res.close();
          }catch(SQLException e){
         System.out.println(e);
    }
    return data;
 }
     
      public void deleteAuto(int cod){  
            try {                
                PreparedStatement pstm = con.getConnection().prepareStatement("delete from autos where Codigo = ?");            
                pstm.setInt(1, cod);                   
                pstm.execute();
                pstm.close();            
            }catch(SQLException e){
            System.out.println(e);
            }            
   }
      
      public void updateAutos(String Codigo, String marca, String modelo, String color, String precio){
       try {            
            PreparedStatement pstm = con.getConnection().prepareStatement("update autos " +
            "set Marca = ? ,"  +
            "Modelo = ? ," +
            "Color = ? ," +
            "Precio = ? "+        
            "where Codigo = ? ");            
            pstm.setString(1, marca);                   
            pstm.setString(2, modelo);
            pstm.setString(3, color);
            pstm.setString(4, precio);
            pstm.setString(5, String.valueOf(Codigo));
            pstm.execute();
            pstm.close();  
         }catch(SQLException e){
         System.out.println(e);
      }
   }
}
